package com.fy.utils.aliyunUtils;

import com.fy.utils.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Component
@Slf4j
public class AliOssFileOperation {
    @Autowired
    private AliOssUtil aliOssUtil;




    /**上传阿里云存储文件
     *
     * @param file
     * @param fileName
     * @param userEmail
     * @return
     */
    public List<String> loadFile(MultipartFile file, String fileName, String userEmail) {

        List<String> loadResultList = new ArrayList<>();

        String CodeEmailPart= StringTools.getEmailPrefix(userEmail);
        try {
            log.info("文件大小:{}", file.getSize());
            //截取原始文件名的后缀   dfdfdf.png
            String extension = fileName.substring(fileName.lastIndexOf("."));
            log.info("文件名后缀:{}", extension);
            //构造新文件名称
            String newFileName = CodeEmailPart+"ADD"+UUID.randomUUID().toString().substring(0, 6);
            String objectName = newFileName + extension;
            log.info("新文件名:{}", objectName);
            //文件的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);

            loadResultList.add(filePath);
            loadResultList.add(objectName);
            return loadResultList;
        } catch (IOException e) {
            log.info("文件上传失败：{}", e);
            return null;
        }
    }


    /**上传头像
     *
     * @param file
     * @param userEmail
     * @return
     */
    public String loadAvatar(MultipartFile file,  String userEmail) {

        log.info("file:{}",file);
        //List<String> loadResultList = new ArrayList<>();
        String originFileName = file.getOriginalFilename();
        try {
            log.info("头像文件大小:{}", file.getSize());
            //截取原始文件名的后缀   dfdfdf.png
            String extension = originFileName.substring(originFileName.lastIndexOf("."));
            log.info("文件名后缀:{}", extension);
            //构造新文件名称
            String newFileName = userEmail.split("@")[0];
            String objectName = newFileName + extension;
            log.info("新文件名:{}", objectName);
            //文件的请求路径
            String filePath= aliOssUtil.upload(file.getBytes(), objectName);
            return filePath;
        } catch (IOException e) {
            log.info("文件上传失败：{}", e);
            return null;
        }
    }

    /**
     * 删除阿里云OSS中的文件
     * @param objectName 要删除的文件名
     * @return 如果删除成功返回true，否则返回false
     */
    public boolean deleteFile(String objectName) {
        log.info("Deleting object: {}", objectName);
        try {
            aliOssUtil.delete(objectName);
            log.info("阿里云存储数据删除成功: {}", objectName);
            return true;
        } catch (Exception e) {
            log.error("阿里云存储数据删除失败: {}, error: {}", objectName, e.getMessage());
            return false;
        }
    }
}

